<?php
/**
 * 团检管理
 *
 * User: kaikaic
 * Date: 2018/10/25
 * Time: 17:46
 */

namespace app\exam\controller;

use app\exam\service\EnterpriseService;
use app\exam\service\GroupService;
use app\exam\service\GroupSetService;
use app\exam\service\MecService;
use controller\BasicAdmin;
use service\ConfigService;
use think\Db;

class Group extends BasicAdmin {

    protected $table = "ExamGroup";

    public function index() {
        $this->title = "团检申请";
        $query = array();
        $query['title'] = $this->request->get("title");

        $db = Db::name($this->table);
        if ($query['title']) {
            $db->whereLike('title', "%{$query['title']}%");
        }

        return parent::listRecord($db);
    }

    /**
     * 团检计划-新增
     */
    public function add() {
        if ($this->request->isGet()) {
            $vo = array();
            $vo['start_time'] = date("Y-m-d", strtotime("+1 day"));
            $vo['end_time'] = date("Y-m-d", strtotime("+1 month"));

            return view('form', [
                'title'=>'新增团检申请',
                'vo'=>$vo,
                'mecs'=>MecService::getInstance()->listMec(),
                'enterprises'=>EnterpriseService::getInstance()->listEnterprise(),
                'group_set'=>GroupSetService::getInstance()->listGroupSet()
            ]);
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();

            if ($this->doInsertAction($data)) {
                $this->success('新增成功！', "");
            }else {
                $this->error('新增失败，请稍候再试！');
            }
        }
    }

    /**
     * 团检计划-编辑
     */
    public function edit() {
        if ($this->request->isGet()) {
            $id = $this->request->get('id', '');
            if (empty($id)) {
                $this->error('参数错误，请稍候再试！');
            }

            $record = GroupService::getInstance()->find($id);
            if (empty($record)) {
                $this->error('记录不存在，请重新操作!');
            }

            return view('form', [
                'title'=>'编辑团检申请',
                'vo'=>$record,
                'mecs'=>MecService::getInstance()->listMec(),
                'enterprises'=>EnterpriseService::getInstance()->listEnterprise(),
                'group_set'=>GroupSetService::getInstance()->listGroupSet()
            ]);
        }

        if ($this->request->isPost()) {
            $id = $this->request->post('id', '');
            if (empty($id)) {
                $this->error('参数错误，请稍候再试！');
            }

            $data = $this->request->post();
            if ($this->doInsertAction($data)) {
                $this->success('编辑成功！', '');
            }else {
                $this->error('编辑失败，请稍候再试！');
            }
        }
    }

    /**
     * 团检计划-删除
     */
    public function remove() {
        $id = intval($this->request->post('id', ''));
        if (empty($id)) {
            $this->error('参数错误!');
        }

        $record = GroupService::getInstance()->find($id);
        if (empty($record)) {
            $this->error('记录不存在，请重新操作!');
        }

        $result = GroupService::getInstance()->remove(array('id'=>$id));
        if ($result) {
            $this->success('删除成功!', '');
        }else {
            $this->error('删除失败，请重新再试!');
        }
    }

    /**
     * 列表数据处理
     */
    protected function _index_data_filter(&$data) {
        $size = count($data);
        $cfgStatus = ConfigService::statusConfig();
        $enterprises = EnterpriseService::getInstance()->listEnterprise();
        $mecList = MecService::getInstance()->listMec();
        $groupSetList = GroupSetService::getInstance()->listGroupSet();

        for ($i=0; $i<$size; $i++) {
            $temp = $data[$i];

            $temp['mec'] = "";
            if ($temp['mec_id']) {
                $temp['mec'] = $mecList[$temp['mec_id']];
            }

            $temp['enterprise'] = $enterprises[$temp['enterprise_id']];

            $temp['group_set'] = "";
            if ($temp['group_set_id']) {
                $temp['group_set'] = $groupSetList[$temp['group_set_id']];
            }

            $temp['status_txt'] = $cfgStatus[$temp['status']];

            $data[$i] = $temp;
        }

        $this->assign('enterprise_list', $enterprises);
        $this->assign('mec_list', $mecList);
        $this->assign('group_set_list', $groupSetList);
        $this->assign('cfg_status', $cfgStatus);
    }

    /**
     * 数据写入
     */
    protected function doInsertAction($data) {
        if (empty($data['id'])) {
            $data['status'] = 1;
            $data['create_time'] = time();

            $result = GroupService::getInstance()->insert($data);
        } else {
            $id = intval($data['id']);
            $result = GroupService::getInstance()->update($data, $id);
        }

        return $result;
    }
}

